<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>登录 | VigorLoop</title>
  <meta name="description" content="VigorLoop 健康管理系统">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://www.jq22.com/jquery/bootstrap-4.2.1.css">
  <link rel="stylesheet" href="/static/bootstrap/css/style.default.css" id="theme-stylesheet">
  <style>
    /* 消息框样式调整 */
    .alert {
      position: fixed;
      top: 10px;
      left: 50%;
      transform: translateX(-50%);
      max-width: 400px;
      z-index: 9999; /* 保证消息框在最前面 */
    }

    /* 防止消息框占用空间，设置透明的背景 */
    .alert-container {
      position: absolute;
      width: 100%;
      top: 0;
      left: 0;
      right: 0;
      pointer-events: none; /* 防止干扰其他操作 */
    }

    /* 避免影响表单，增加表单的层级 */
    .form-holder {
      position: relative;
      z-index: 1;
    }
  </style>
</head>
<body>
  <div class="page login-page">
    <div class="container d-flex align-items-center">
      <div class="form-holder has-shadow">
        <div class="row">
          <!-- Logo & Information Panel-->
          <div class="col-lg-6">
            <div class="info d-flex align-items-center">
              <div class="content">
                <div class="logo">
                  <h1>欢迎登录</h1>
                </div>
                <p>VigorLoop 健康管理系统</p>
              </div>
            </div>
          </div>
          <!-- Form Panel -->
          <div class="col-lg-6 bg-white">
            <div class="form d-flex align-items-center">
              <div class="content">
                <!-- Login form -->
                <form method="POST" action="{% url 'login' %}" class="form-validate" id="loginForm">
                  {% csrf_token %}

                  <!-- Messages Section -->
                  <div class="alert-container">
                    {% if messages %}
                      <div class="container">
                        {% for message in messages %}
                          <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                            {{ message }}
                          </div>
                        {% endfor %}
                      </div>
                    {% endif %}
                  </div>

                  <!-- Username Input -->
                  <div class="form-group">
                    <input id="login-username" type="text" name="username" required data-msg="请输入用户名" placeholder="用户名" class="input-material">
                  </div>

                  <!-- Password Input -->
                  <div class="form-group">
                    <input id="login-password" type="password" name="password" required data-msg="请输入密码" placeholder="密码" class="input-material">
                  </div>

                  <!-- Login Button -->
                  <button id="login" type="submit" class="btn btn-primary">登录</button>

                  <!-- Remember Me Checkbox -->
                  <div style="margin-top: -40px;">
                    <div class="custom-control custom-checkbox" style="float: right;">
                      <input type="checkbox" class="custom-control-input" id="check1">
                      <label class="custom-control-label" for="check1">记住密码&nbsp;&nbsp;</label>
                    </div>
                  </div>
                </form>

                <!-- Register Link -->
                <br />
                <small>没有账号?</small><a href="/register" class="signup">&nbsp;注册</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- JavaScript files-->
  <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
  <script src="https://www.jq22.com/jquery/bootstrap-3.3.4.js"></script>
  <script src="/static/bootstrap/vendor/jquery-validation/jquery.validate.min.js"></script><!-- 表单验证 -->
  <script src="/static/bootstrap/js/front.js"></script>

  <script>
    $(function(){
      /* 判断是否勾选记住密码 */
      var check1s = localStorage.getItem("check1");
      var oldName = localStorage.getItem("userName");
      var oldPass = localStorage.getItem("passWord");

      if (check1s == "true") {
        $("#login-username").val(oldName);
        $("#login-password").val(oldPass);
        $("#check1").prop('checked', true);
      } else {
        $("#login-username").val('');
        $("#login-password").val('');
        $("#check1").prop('checked', false);
      }

      /* 登录时保存用户名和密码 */
      $("#login").click(function(){
        var userName = $("#login-username").val();
        var passWord = $("#login-password").val();
        /* 获取当前输入的账号密码 */
        localStorage.setItem("userName", userName);
        localStorage.setItem("passWord", passWord);

        /* 获取记住密码 checkbox 的值 */
        var check1 = $("#check1").prop('checked');
        localStorage.setItem("check1", check1);
      })

      // 自动隐藏消息框
      $(".alert").delay(3000).fadeOut(500); // 3秒后自动消失
    })
  </script>
</body>
</html>
